<template>
	<view>
		  <!-- #ifdef MP-WEIXIN -->
		  <view class="header">
		  <!-- #endif -->
		  <!-- #ifdef H5 -->
		  <view class="header header-top-h5">
		  <!-- #endif -->
		  	<view class="left">
		  		<image :src="userInfo?userInfo.avatarUrl:'https://www.atuanjian.com/appletStatic/static/mine/head.png'" mode="aspectFill"></image>
		  		<view class="user" v-if="status==1">
		  			<text>{{userInfo.nickName}}</text> 
		  		</view>
		  		<view class="login" v-else>
		  			<u-button  @click="Login" type="primary" text="切换账号"></u-button>
		  		</view>
		  	</view>
		  	<view class="right">
		  		<image src="https://www.atuanjian.com/appletStatic/static/mine/i11.png" style="opacity: 0;" mode="aspectFit"></image>
		  	</view>
		  </view>
			<view class="items">
				<view class="left">手机号</view>
				<view class="right">
					<input type="text" v-model="phone" value="" placeholder="请输入" />
					<!-- <image class="arrow_right" src="https://www.atuanjian.com/appletStatic/static/right.png" mode=""></image> -->
				</view>
			</view>
			<view class="items">
				<view class="left">手机验证码</view>
				<view class="right">
					<input type="text" v-model="security_code" value="" placeholder="请输入" /> 
					<view class="code" v-if="isSend" @click="getCode">获取验证码</view>
				</view>
			</view>
			<view class="items">
				<view class="left">登录密码</view>
				<view class="right">
					<input type="text" v-model="password" value="" placeholder="请输入" /> 
				</view>
			</view> 
			
			   
		
		<view class="footer">
			<view class="submit" @click="sure">确定</view>
		</view> 
		
	</view>
</template>

<script>
	import {sendCode,switchAccount} from '@/utils/request.js'
	
	export default {
		data() {
			return {
				// isLog:
				status:0,
				userInfo:JSON.parse(uni.getStorageSync('userInfo')||{}),
				isSend:true,
				phone:'',
				password:'', 
				security_code:'',
				code:''
			}
		},
		methods: {
			Login(){
				let t=this
				uni.getUserProfile({
					desc: "用于登录",
					lang: "zh_CN",
					success: res1 => {
						uni.showLoading({
							title: "登陆中..."
						}); 
						uni.login({
							success: res2 => { 
								t.code= res2.code 
								t.status=1
								uni.hideLoading()
							},
							fail: err => {
								// this.isLog = true;
							}
						});
					},
					fail: err => {
						uni.showToast({
							title: "拒绝访问个人信息可能会导致部分功能无法正常使用",
							icon: "none",
							duration: 3000
						});
						// this.isLog = true;
					}
				});
			},
			sure(){
				
				if(!this.code){
					uni.showToast({
						title:'请切换账号',
						icon:'none'
					})
					return;
				}
				if(!this.phone.trim()){
					uni.showToast({
						title:'请输入手机号',
						icon:'none'
					})
					return;
				}
				if(!this.security_code.trim()){
					uni.showToast({
						title:'请输入验证码',
						icon:'none'
					})
					return;
				}
				if(!this.password.trim()){
					uni.showToast({
						title:'请输入密码',
						icon:'none'
					})
					return;
				} 
				let data={
					token:this.userInfo.token,
					code:this.code,
					phone:this.phone,
					password:this.password,
					security_code:this.security_code
				}
				this.switchAccount(data).then(res=>{
					uni.showToast({
						title:'修改完成',
						duration:1500
					})
					setTimeout(()=>{
						uni.navigateBack({
							delta:1
						})
					},1500)
				})
			},
			async switchAccount(data){
				return await switchAccount(data)
			},
			async sendCode(data){
				return await sendCode(data)
			},
			getCode(){
				if(!this.phone.trim()){
					uni.showToast({
						title:'请输入手机号',
						icon:'none',
						duration:2000
					})
					return
				}else if(!/^1[3-9]\d{9}$/.test(this.phone.trim())){
					uni.showToast({
						title:'手机号格式有误',
						icon:'none',
						duration:2000
					})
					return
				}else{ 
					if(!this.isSend){
						return
					}
					this.isSend=false
					let data={
						phone:this.phone.trim()
					}
					this.sendCode(data).then(res=>{
						uni.showToast({
							title:'验证码10分钟内有效',
							icon:'none',
							duration:2000
						})
						this.color='#CCCCCC'
					}).catch(err=>{
						console.log(err)
					})
				}
			},
		}
	}
</script>

<style lang="scss">
page{
	background-color: #F5F5F5;
}
 .login{
	::v-deep .u-button--primary{
 		background-color: #FFFFFF!important;
 		border-width: 0!important;
 	}
 	::v-deep .u-button__text{
 		color: #333333;
 	}
 }
.header{
	padding: 60rpx 30rpx 60rpx 60rpx;
	background-color: #FFFFFF;
	@include flex_style(space-between,center,row nowrap);
	.left{
		@include flex_style(flex-start,center,row nowrap);
		image{
			width: 120rpx;
			height: 120rpx;
			margin-right: 28rpx;
			border-radius: 50%;
		}
		.user{
			max-width: 400rpx;
			@include flex_style(flex-start,flex-start,column nowrap);
			text{
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 600;
				line-height: 40rpx;
				color: #333333;
			}
			.phone{
				margin-top: 20rpx;
				font-size: 24rpx;
			}
		}
		
	} 
	.right{ 
		image{
			width: 36rpx;
			height: 36rpx;
		}
	}
}
.head{
	background-color: #FFFFFF;
	background-color: #FFFDFD;
	border-radius: 16rpx;
	padding: 40rpx 0;
	.title{
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 400;
		line-height: 40rpx;
		color: #1A1A1A;
		margin-bottom: 32rpx;
			text-align: center;
	}
	.select{
		display: flex;
		justify-content: center;
		align-items: center;
		.item{
			width: 120rpx;
			height: 40rpx;
			text-align: center;
			line-height: 40rpx;
			background: #EBEBEB; 
			border-radius: 4rpx;
			font-size: 22rpx;
			font-family: PingFang SC;
			font-weight: 400; 
			color: #1A1A1A;
			margin: 0 20rpx;
		}
		
		.active{
			background-color: #FFB850;
			color: #FFFFFF;
		}
	}
	
}
.code{
	border-radius: 8rpx;
	padding: 4rpx 8rpx;
	background-color:#FFB850 ;
	color: #FFFFFF;
	font-size: 24rpx;
	line-height: 30rpx;
	margin-left: 16rpx;
}
.items{
	background-color: #FFFFFF;
	padding: 24rpx;
	display: flex;
	justify-content: space-between;
	align-items: center;
	border-bottom: 1px solid #F5F5F5;
	.left{
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 400;
		line-height: 40rpx;
		color: #1A1A1A;
	}
	.right{
		display: flex;
		justify-content: flex-start;
		align-items: center;
		input{
			margin: 0;
			padding: 0;
			border: none;
			outline: none;
			background-color: #FFFFFF;
			text-align: right;
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: 400;
			line-height: 40rpx;
			color: #999999;
		}
	}
}
.arrow_right{
	width: 20rpx;
	height: 20rpx;
}
.footer{ 
		margin-top: 120rpx;
		.submit{
			width: 670rpx;
			height: 92rpx;
			background: linear-gradient(98deg, #FFB850 0%, #FF8000 100%);
			opacity: 0.92;
			text-align: center;
			border-radius: 46rpx;
			margin: 0 auto;
			font-size: 32rpx;
			font-family: PingFang SC;
			font-weight: 400;
			line-height: 92rpx;
			color: #FFFFFF;
		}
	}
	.desc{
		text-align: center;
		margin-top: 24rpx;
		font-size: 24rpx;
		font-family: PingFang SC;
		font-weight: 400;
		line-height: 34rpx;
		color: #888888;
	}
</style>
